package com.xintonghua.base.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import java.util.List;

/**
 * Created by hao on 2017/2/23.
 */
public abstract class BaseAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
    protected Activity mActivity;
    protected List<T> list;
    protected LayoutInflater inflater;
    protected IAdapter ife;

    public List<T> getList() {
        return list;
    }

    public BaseAdapter(Activity mActivity, List<T> list) {
        this.mActivity = mActivity;
        this.list = list;
        inflater = LayoutInflater.from(mActivity);
    }

    public BaseAdapter(IAdapter ife, List<T> list) {
        this.ife = ife;
        this.mActivity = ife.getContext();
        this.list = list;
        inflater = LayoutInflater.from(mActivity);
    }

    public void setList(List<T> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addAll(List<T> l) {
        if (list == null || l == null) {
            return;
        }
        list.addAll(l);
        notifyDataSetChanged();
    }

    public void clear() {
        if (list == null) {
            return;
        }
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface IAdapter {
        Activity getContext();

        void itemClick(View view, int position);
    }


}
